import {request} from '@/api/service'
import {GetChildren, GetTree} from "./api";

export const crudOptions = (vm) => {
  return {
    rowHandle: {
      view: {
        thin: true,
        text: null,
      },
      edit: {
        thin: true,
        text: null,
      },
      remove: {
        thin: true,
        text: null,
      },
      custom: [
        {
          thin: true,
          text: null,
          type: 'primary',
          size: 'small',
          emit: 'add-sub-resource',
          icon: 'el-icon-plus',
        }
      ],
      width: 180,
      fixed: 'right'
    },
    options: {
      // height: '100%', // 表格高度100%, 使用toolbar必须设置
      highlightCurrentRow: true,
      rowKey: 'id'
    },
    columns: [
      {
        title: 'ID',
        key: 'id',
        disabled: true,
        form: {
          component: {
            show: false
          }
        }
      },
      {
        title: '部门名称',
        key: 'name',
      },
      {
        title: '部门代码',
        key: 'code',
      },
      {
        title: '上级部门',
        key: 'pid',
        sortable: true,
        type: 'tree-selector',
        dict: {
          url: '/dept/tree',
          isTree: true,
          value: 'id',
          label: 'name',
          getData: (url, dict) => { // 配置此参数会覆盖全局的getRemoteDictFunc
            return request({url: url}).then(ret => {
              return [{id: 0, name: '根节点', children: ret.data}]
            })
          }
        },

        form: {
          component: {
            props: {
              multiple: false,
              elProps: {
                lazy: true,
                defaultExpandAll: true,
                load (node, resolve) { // 懒加载
                  if(node.level === 0){
                    GetChildren().then(res=>{
                      resolve([{id: 0, name: '根节点', children: res.data}])
                    })
                  }else {
                    let editForm = vm.getEditForm()
                    let exclude = editForm.id;
                    GetChildren({id:node.data.id, exclude}).then(res=>{
                      resolve(res.data)
                    })
                  }
                  // console.log(node)
                  // if(node.hasChildren){
                  //   GetChildren({id: node.id}).then(res=>{
                  //     resolve(res.data)
                  //   })
                  // }
                }
              },
              dict: {cache: false}
            }
          }
        }
      }
    ]
  }
}
